import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class GetGroupVolume {    

    private String groupId   = null;
    private String groupName = null;
    private String tableName = null;
    public static void main (String args[]) {

	HttpTableDataLoader d_loader = new HttpTableDataLoader ();
	String url_return_str = null;
	Vector url_return_v  = new Vector ();
	Vector record_head_v = new Vector ();
	Vector record_v      = new Vector ();
	Vector record_v_v    = new Vector ();
	String url = null;
	String url_semi   = null;

	if (args.length<1) {

	    MyPrint.printLine("=",60);
	    System.out.println("[0] Save Naz $5.00-1000 Into Table sector_record");
	    System.out.println("Use SectorChange 4 To run report");
	    System.out.println("[1][sector_record1]::$0.01-0.99");
	    System.out.println("[2][sector_record2]::$1.00-4.99");
	    System.out.println("[3][sector_record3]::$5.00-9.99");
	    System.out.println("[4][sector_record4]::$10.00-29.99");
	    System.out.println("[5][sector_record5]::$30+");	    
	    MyPrint.printLine("=",60);
	    System.exit(0);
	}

	String chioce = args[0];

	url="https://www.bigcharts.com/custom/datek-com/screener/screener.asp?"+
	    "sh=1%2C1%2C1%2C1%2C1%2C1%2C1&pg=vop&"+
	    "cols=0&cols=4&cols=8&cols=1&cols=5&cols=9&cols=2&cols=6&cols=10&cols=3&cols=7&"+
	    "sort=0&sortd=1&numresults=2500&s1=1&s1p1=5&s1p2=10000&s2p1=0&s2p3=0&s4p2=0&s9=1&"+
	    "s9p1=1000&s9p2=99999999999&exchange=15&result.x=44&result.y=9";

	String table_name[] = {"sector_record","sector_record1","sector_record2","sector_record3",
			       "sector_record4","sector_record5"};

	GetGroupVolume gv = new GetGroupVolume ();

	d_loader.setUrl(url);
	d_loader.connectToUrl();
	d_loader.setStartString("Symbol");
	d_loader.setEndString("Previous");
	url_return_str = d_loader.getUrlReturnString();
	d_loader.stringToVector(url_return_str);
	url_return_v = d_loader.getUrlReturnVector(); 
	//MyPrint.printVector(url_return_v,1);
	url_return_v = d_loader.selectUsefulElements(url_return_v);
	//MyPrint.printVector(url_return_v,1);
	//url_return_v = gv.localVectorFilter(url_return_v);
	d_loader.setRecordVectorHold(url_return_v);
	record_head_v = d_loader.getRecordHeadVector();
	//MyPrint.printVector(record_head_v,1);
	//MyPrint.printVector(url_return_v,1);
	record_v_v    = d_loader.getRecordVectorHold();

	/**
	 * Load Group Id and Group Name Into Hashtable
	 */

	Hashtable group_hashtable = new Hashtable ();
	GroupProfileLoader loader = new GroupProfileLoader();
	loader.setFileName("./Data/Group/IndustryGroup.txt");
	loader.loadProfile();
	group_hashtable = loader.getHashTable();

	/**
	 * Get current date and time
	 */

	DateAndTime dt = new DateAndTime ();

	/**
	 * Load tick file by group id
	 */
	String group_id   = null;
	String group_name = null;
	String group_tick_file_name = null;
	Vector grouped_v_v_v = new Vector();

	String date = null;
	String time = null;

	Enumeration enum_id = group_hashtable.keys();  	   
	while (enum_id.hasMoreElements()) {
	    group_id   = (String)enum_id.nextElement();
	    group_name = (String)group_hashtable.get(group_id);

	    MyPrint.printLine("=",80);	
	    System.out.println("\t*******"+"   group_id="+group_id+"\t"+group_name+"  *******");

	    /**
	     * Use group Id to load group tick file
	     */

	    group_tick_file_name = "./Data/Group/"+group_id.trim()+".txt";

	    Hashtable group_tick_hashtable = new Hashtable ();
	    TickFileLoader tf_loader = new TickFileLoader();

	    tf_loader.setFileName(new File(group_tick_file_name));
	    tf_loader.loadFile();
	    group_tick_hashtable = tf_loader.getHashTable();

	    Vector grouped_record_v = new Vector();
	    String tick = null;

	    /**
	     * Use group tick hashtable to find vector contains this tick
	     */

	    Enumeration enum_tick = group_tick_hashtable.keys();  	   
	    while (enum_tick.hasMoreElements()) {
		tick   = (String)enum_tick.nextElement();

		Vector temp_vector = new Vector();
		Enumeration enum = (d_loader.getRecordVectorHold()).elements();

		while (enum.hasMoreElements()) {
		    temp_vector = (Vector)enum.nextElement();
		    if (temp_vector.contains(tick)){
			//System.out.println("Found tick "+tick);
			grouped_record_v.addElement(temp_vector);
		    }
		}    
	    }

	    String total_v         = null; 
	    String total_v_up      = null;
	    String total_v_down    = null; 
	    String total_v_unch    = null; 

	    String total_s      = null;
	    String total_s_up   = null;
	    String total_s_down = null;
	    String total_s_unch = null;


	    String query_str = null;
	    Vector sector_v = new Vector ();
	    Vector sector_v_up = new Vector ();
	    Vector sector_v_down = new Vector ();
	    Vector sector_v_unch = new Vector ();

	    sector_v_up = d_loader.sumColumnValue(grouped_record_v, 5, "long","+",3);

	    total_v_up = (String)sector_v_up.firstElement();
	    total_s_up = (String)sector_v_up.lastElement();

	    sector_v_down = d_loader.sumColumnValue(grouped_record_v, 5, "long","-",3);

	    total_v_down = (String)sector_v_down.firstElement();
	    total_s_down = (String)sector_v_down.lastElement();

	    sector_v_unch = d_loader.sumColumnValue(grouped_record_v, 5, "long","UN",3);

	    total_v_unch = (String)sector_v_unch.firstElement();
	    total_s_unch = (String)sector_v_unch.lastElement();

	    sector_v = d_loader.sumColumnValue(grouped_record_v, 5, "long","any",3);

	    total_v = (String)sector_v.firstElement();
	    total_s = (String)sector_v.lastElement();
	   
	    query_str = "'"+dt.getCurrentDate()+"',";
	    query_str = query_str + "'" + dt.getCurrentTime()+"',";
	    query_str = query_str + "'" + group_name +"',";
	    query_str = query_str + total_s +",";
	    query_str = query_str + total_s_up +",";
	    query_str = query_str + total_s_down +",";
	    query_str = query_str + total_s_unch +",";
	    query_str = query_str + total_v +",";
	    query_str = query_str + total_v_up +",";
	    query_str = query_str + total_v_down +",";
	    query_str = query_str + total_v_unch +",";
	    query_str = query_str + "'" + group_id +"'";

	    MyPrint.printLine("-",80);	
	    System.out.println("V Up   ="+total_v_up);
	    System.out.println("V Down ="+total_v_down);
	    System.out.println("V Unch ="+total_v_unch);
	    System.out.println("V Total="+total_v);
	    MyPrint.printLine(".",80);

	    System.out.println("S Up   ="+total_s_up);
	    System.out.println("S Down ="+total_s_down);
	    System.out.println("S Unch ="+total_s_unch);
	    System.out.println("S Total="+total_s);

	    System.out.println("query_str = "+query_str);
	    gv.setTableName(table_name[0]);
	    gv.insertIntoDataBase(query_str);
	}
    }

    private Vector localVectorFilter (Vector v_in) {

	Vector v_out = new Vector (10);
	String text = null;
	Enumeration penum = v_in.elements();
	
	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	   	    
	    if ( ! (text.startsWith("<") )) {
		v_out.addElement(text);
	    }
	}
	
	return v_out;
    }

    private String localStringFilter (String url_return) {
	return url_return;
    }

    private void setTableName(String table_name) {
	tableName = table_name;
    }

    private void insertIntoDataBase( String queryStr) {
	int tryTime =0;
	StringBuffer queryBf = new StringBuffer("INSERT INTO "+tableName +" VALUES (");

	queryBf.append(queryStr+");");

	DataBaseInsert myInsert = new DataBaseInsert();	
	
	myInsert.setQueryString(queryBf.toString());
	myInsert.insertData();
    } 
}










